package com.example.stream;

import java.util.Arrays;
import java.util.List;

/**
 * Created by butterfly on 2018/7/4.
 */
public class FlatMapDemo {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("hello","world");
        //用扁平化流截取不重复的字母
        list.stream().map(s -> s.split("")).flatMap(strings -> Arrays.stream(strings)).distinct().forEach(System.out::println);//截完后是2个数组，分别通过Arryas.stream()变成两个流,再用flatMap串成一个流
        System.out.println("============");

        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
        integers.stream().map(i -> {
            return i*i;
        }).forEach(System.out::println);
        /*integers.stream().forEach(integer -> {
            integer = integer*integer;
        });*/
        integers.stream().forEach(integer -> System.out.println(integer+"***"));
        //用扁平化流处理[1,2,3][3,4]返回[(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
        List<Integer> numbers1 = Arrays.asList(1, 2, 3);
        List<Integer> numbers2 = Arrays.asList(3, 4);
        numbers1.stream().flatMap(i -> {
         return   numbers2.stream().map(j->{
               return new int[]{i,j};
           });
        }).forEach(System.out::println);

        String test = test();
        System.out.println(test);
    }

    public static  String test(){
        try {
            return "1";
        }finally {
            return "2";
        }
    }
}
